package me.abel.quartz.jobs;

import com.alibaba.fastjson.JSON;
import me.abel.quartz.config.SpringContextUtil;
import me.abel.quartz.service.AccessTokenService;
import me.abel.quartz.service.impl.AccessTokenServiceImpl;
import org.quartz.*;

import java.io.Serializable;

@DisallowConcurrentExecution
public class AccessTokenJob implements Job, Serializable {

    private static final long serialVersionUID = 1L;

    @Override
    public void execute(JobExecutionContext context){
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap = jobDetail.getJobDataMap();

        System.out.println(JSON.toJSONString(dataMap));
        AccessTokenService accessTokenService = SpringContextUtil.getBeanByClass(AccessTokenServiceImpl.class);

        System.out.println(JSON.toJSONString(accessTokenService.getRemote(dataMap.getString("corpId"), dataMap.getString("secret"))));

//        /**
//         * 获取任务中保存的方法名字，动态调用方法
//         */
//        String methodName = dataMap.getString("jobMethod");
//        try {
//            AccessTokenJob job = new AccessTokenJob();
//            Method method = job.getClass().getMethod(methodName);
//            method.invoke(job);
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
    }
}